<?
//Block 0.0.1
class sym_block extends symbiont{
    public function ___main($design='', $id=''){
        if(!data::is_uint($id)) return;
        global $kernel, $sql;
        $kernel->library_add('paragraph');
        if($design!='') $design='blocks/'.$design;
        $design=$this->_check($design, 'blocks/main');
        $d=new design($design, 'block', false);
        
        $title_pid=$sql->select_each('sym_block', 'title_pid', array('id'=>$id), '', '', 1);
        $p=new paragraph($title_pid);
        $p->read();
        $this->title=$p->title;
        $this->text=$p->content;
        
        ob_start();
        $pos=new positions('', 'block.'.$id);
        $pos->prints();
        $this->content=ob_get_clean();
        
        $d->inc();
    }
    public function ___admin($design=''){
        
    }
    public function _administrate($symbiont){
        if($symbiont['full']=='block'){
            $design=$this->_check('administrate_save', 'administrate_save');
            $d=new design($design, 'block');
        }
        else{
            global $kernel, $sql;
            
            $design=$this->_check('administrate', 'administrate');
            $d=new design($design, 'block', false);
            
            $kernel->library_add('paragraph');
            
            $id=$symbiont['function'];
            $title_pid=$sql->select_each('sym_block', 'title_pid', array('id'=>$id), '', '', 1);
            
            $this->content='';
            $this->id=$id;
            $this->symbiont=$symbiont['full'];
            
            $this->infos='';
            $this->languages='';
            $languages=$sql->select_each('languages', 'abbr', array('is_enabled'=>1), '', 'position');
            $p=new paragraph($title_pid);
            foreach($languages as $val){
                $this->abbr=$val['abbr'];
                $p->read($val['id']);
                $this->title=$p->title;
                $this->content=$p->content;
                $this->languages.=design::run($this->language);
                $this->infos.=design::run($this->info);
            }
            
            $d->inc();
        }
    }
    public function _add($symbiont){
        global $kernel, $sql;
        $kernel->library_add('paragraph');
        $p=new paragraph();
        $p->add();
        $id=$sql->insert('sym_block', array('title_pid'=>$p->id));
        return $symbiont['full'].'.'.$id;
    }
    public function _delete($symbiont){
        global $kernel;
        $kernel->library_add('paragraph');
        $title_pid=$sql->select_each('sym_block', 'title_pid', array('id'=>$symbiont['function']), '', '', 1);
        $sql->delete('sym_block', array('id'=>$symbiont['function']));
        $p=new paragraph($title_pid);
        $p->del();
    }
    public function ___db_save($design){
        global $sql, $user, $kernel;
        if($user->access_level<9||!isset($_POST['title'])||!isset($_POST['content'])||!isset($_POST['id'])) return;
        $kernel->library_add('paragraph');
        $id=data::number($_POST['id']);
        $title_pid=$sql->select_each('sym_block', 'title_pid', array('id'=>$id), '', '', 1);
        $p=new paragraph($title_pid);
        foreach($_POST['title'] as $key=>$val){
            $abbr=data::word($key);
            $title=data::safe($val);
            $content=data::safe($_POST['content'][$key]);
            $lng_id=$sql->select_each('languages', 'id', array('abbr'=>$abbr), '', '', 1);
            if($title||$content){
                $p->set($lng_id, $title, $content);
            }
        }
    }
    public function _info($symbiont){
        global $kernel, $sql;
        $kernel->library_add('paragraph');
        $title_pid=$sql->select_each('sym_block', 'title_pid', array('id'=>$symbiont['function']), '', '', 1);
        $p=new paragraph($title_pid);
        $p->read();
        return array('title'=>$p->title, 'block'=>true);
    }
}
?>